<?php

namespace app\common\model;

use fast\Http;
use think\Model;

use think\Image;

class Machine extends Model
{

    

    

    // 表名
    protected $name = 'machine';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'isLeisure_text',
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }
    public function getIsLeisureList()
    {
        return ['0' => __('IsLeisure 0'), '1' => __('IsLeisure 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsLeisureTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['isLeisure']) ? $data['isLeisure'] : '');
        $list = $this->getIsLeisureList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function merchant()
    {
        return $this->belongsTo('Merchant', 'merchant_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function createWxCode($id,$env_version="trial",$wxpath = 'my_pages/codeSweepWash/codeSweepWash'){
        //文件保存路径
        try {
            $path = ROOT_PATH . 'public/uploads/wxcode/';
            if (!is_dir($path)) {
                @mkdir($path);
            }
            $fileName = 'machine_' . $id;
            $wxCodeUrl = $path . '/' . $fileName . ".png";
            /*if (file_exists($fileName)){
                return ['code' => 1];
            }*/
            $acctoken=getAccessToken();
            $url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$acctoken["access_token"];
            $data=[
                'scene'=> $id,
                'page' => $wxpath,
                'env_version'=>$env_version//区别是否跳转正式版和体验版：正式版为 "release"，体验版为 "trial"，开发版为 "develop
            ];

            $data = json_encode($data);
            $result = Http::sendRequest($url,$data);
            if (!$result['ret']) {
                return ['code' => 0, 'msg' => '微信创建二维码失败', 'data' => $result];
            }
            file_put_contents($wxCodeUrl,$result["msg"]);
            self::where(["machineID"=>$id])->update([
                "code"=>cdnurl( '/uploads/wxcode/'.$fileName. ".png"),
            ]);
            $url=ROOT_PATH.'public/uploads/wxcode/'.$fileName. ".png";
            $image=Image::open($url);
            $image->text($id,'./assets/fonts/verdana.ttf',18,'#00FF7F',\think\Image::WATER_SOUTHEAST)->save($url);
            return ['code' => 1,'url'=>'public/uploads/wxcode/'.$fileName. ".png"];
        } catch (Exception $e) {
            //Log::record('make wx img error : ' . $e->getMessage());
            return ['code' => 0, 'msg' => $e->getMessage()];
        }

    }
}
